package cn.codeforfun.goods.dao.impl;

import cn.codeforfun.goods.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * Created by Administrator on 2016/12/20 0020.
 */
@Repository
public class GoodsDaoCustomImpl implements GoodsDaoCustom {
    @Autowired
    private EntityManager entityManager;

    @Override
    public Page<Goods> findAll(Pageable pageable) {
        Query query = entityManager.createQuery("select g from Goods g left join fetch g.pics p");
        List resultList = query.setMaxResults(pageable.getPageSize()).setFirstResult(pageable.getPageNumber() * pageable.getPageSize()).getResultList();
        long resultCount = (long) entityManager.createQuery("select count(g.id) from Goods g").getSingleResult();
        return new PageImpl(resultList, pageable, resultCount);
    }
}
